import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'

// 处理SSO登录
export async function handleSsoLogin(ticket: string) {
  const authStore = useAuthStore()
  try {
    await authStore.ssoLogin(ticket)
    return true
  } catch (error) {
    ElMessage.error('SSO登录失败')
    return false
  }
}

// 监听SSO登出消息
export function listenSsoLogout() {
  const authStore = useAuthStore()
  const eventSource = new EventSource(`${import.meta.env.VITE_SSO_URL}/events`)
  
  eventSource.addEventListener('logout', (e) => {
    const data = JSON.parse(e.data)
    if (data.username === authStore.user?.username) {
      authStore.logout()
    }
  })
  
  return () => eventSource.close()
} 